﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapidWebDev.UI.DynamicPages;
using RapidWebDev.UI.Controls;
using TestCenter.Platform.DataModel;
using RapidWebDev.Platform;
using RapidWebDev.Common;
using TestCenter.Platform.Apis;
using RapidWebDev.UI;
using RapidWebDev.Common.Data;

namespace TestCenter.Platform.Web.DynamicPages
{
    public class TestCaseDetail : DetailPanelPage
    {
        protected static readonly IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();

        protected static readonly ITestCenterApi testCenterApi = SpringContext.Current.GetObject<ITestCenterApi>();
        #region Web Controls

        [Binding("TabContainer/TabPanelBody")]
        protected TextBox TextBoxTitle;

        [Binding("TabContainer/TabPanelBody")]
        protected TextBox TextBoxSummary;

        [Binding("TabContainer/TabPanelBody")]
        protected TextBox TextBoxStep;

        [Binding("TabContainer/TabPanelBody")]
        protected TextBox TextBoxExpected;
        #endregion

        public override string Create()
        {
            TestCase entity = new TestCase() { Name = TextBoxTitle.Text, Summary = TextBoxSummary.Text, Steps = TextBoxStep.Text, Expected = TextBoxExpected.Text, 
                CreatedBy = authenticationContext.User.UserId,
                CreatedOn = DateTime.Now,ExecuteType = 1,Improtance = 2 };

            return testCenterApi.SaveTestCase(entity, authenticationContext.TempVariables["suite"].ToString());           

        }

        public override void LoadReadOnlyEntity(string entityId)
        {
            this.LoadWritableEntity(entityId);
            WebUtility.MakeBindingControlsReadOnly(this);
        }

        public override void LoadWritableEntity(string entityId)
        {
            TestCase testcase = testCenterApi.GetTestCaseById(new Guid(entityId));
            TextBoxTitle.Text = testcase.Name;
            TextBoxSummary.Text = testcase.Summary;
            TextBoxStep.Text = testcase.Steps;
            TextBoxExpected.Text = testcase.Expected;
        }

        public override void Update(string entityId)
        {
            using (TestCenterDataContext ctx = DataContextFactory.Create<TestCenterDataContext>())
            {
                TestCase entity = ctx.TestCases.FirstOrDefault(x => x.Id.ToString().Equals(entityId));

                entity.Name = TextBoxTitle.Text;
                entity.Summary = TextBoxSummary.Text;
                entity.Steps = TextBoxStep.Text;
                entity.Expected = TextBoxExpected.Text;

                ctx.SubmitChanges();
            }
        }

        public override void SetupContextTempVariables(IRequestHandler sender, SetupApplicationContextVariablesEventArgs e)
        {
            if (sender.Parameters["suite"] != null)
                authenticationContext.TempVariables["suite"] = new Guid(sender.Parameters["suite"]); ;
        }
    }
}
